var utilsmd5 = require('../../utils/md5.js')
var app = getApp()
Page({
  data: {
    arraydetail: [],
    winWidth: 0,
    winHeight: 0,
    scrollTop: 0,
    scrollHeight: 0,
    pageIndex: 1,
    orderType: 0, //	0＝全部 1=待付款 2=待发货 3=已发货 4=待评价 5=退款
    navbar: ['全部', '待付款', '待发货', '待收货', '已完成'],
    loadding: true,
    author: 'Lotus',
    status:0
  },
  navbarTap: function (e) {
    var that = this;
    console.log("nav");
    
    var status = e.currentTarget.dataset.idx;
    that.setData({
      status: status,
      arraydetail: [],
      pageIndex: 1,
      loadding: true
    })
    console.log(status);
    that.getlist();
  },
  receipt: function (e) {
    var that = this;
    var service = 'Order.receiveOrder';
    var orderId = e.target.dataset.id;
    wx.request({
      method: 'POST',
      url: app.globalData.APIURL,
      data: { service: service, openId: app.globalData.openId, orderId: orderId },
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log("success:" + res.data.data);
        that.setData({
          arraydetail: [],
          pageIndex: 1,
          loadding: true
        })

        wx.showToast({
          title: '收货成功!',
          icon: 'success',
          duration: 2000
        })

        that.getlist();
      },
      fail: function (res) {
        //console.log("fail:" + res);
      },
    })
  },

  shouhuo: function (e) {
    var that = this;
    var service = 'Order.receiveOrder';
    var orderId = e.target.dataset.id;
           console.log("orderId:" +orderId);
    wx.request({
      method: 'POST',
      url: app.globalData.APIURL,
      data: { service: service, openId: app.globalData.openId, orderId: orderId},
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log("success:" + res.data.data);
        that.setData({
          arraydetail: [],
          pageIndex: 1,
          loadding: true
        })

        wx.showToast({
          title: '处理成功',
          icon: 'success',
          duration: 2000
        })
        that.getlist();
      },
      fail: function (res) {
        //console.log("fail:" + res);
      },
    })
  },

  buy: function (e) {
    var that = this;
    var orderId = e.target.dataset.id;
    var otherFee =e.target.dataset.price;

    console.log("buy start");
    var service = 'Order.payForOrder';
    var otherType = 'wxcxjsapipay';
    console.log("openId:" + app.globalData.openId);
    console.log("orderId:" + orderId);
    console.log("otherType:" + otherType);
    console.log("otherFee:" + otherFee);
    wx.request({
      method: 'POST',
      url: app.globalData.APIURL,
      data: { service: service, openId: app.globalData.openId, orderId: orderId, otherType: otherType, otherFee: otherFee },
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      success: function (res) {

        console.log("success:" + res.data.data);

        if (!res.data.data.code) {
          var info = res.data.data.info.tradeNo;
          var param = [];
          param['appid'] = info.appId;
          param['timeStamp'] = info.timeStamp;
          param['nonceStr'] = info.nonceStr;
          param['package'] = info.package;
          param['paySign'] = info.sign;

          // param['prepayid'] = info.prepayid; 
          // param['partnerid'] = info.partnerid;
          that.pay(param);
        } else {
          wx.showToast({
            title: res.data.data.message,
            duration: 2000
          })
        }
      },
      fail: function (res) {
        console.log("fail:" + res);
      },
    })
    console.log("buy end");
  },

  pay: function (param) {
    var that = this;
    console.log("支付 start");
    console.log(param)
    var appid = app.globalData.APPID;
    var timeStamp = '' + param.timeStamp;
    var nonceStr = param.nonceStr;
    var pkg = param.package;
    var signType = 'MD5';
    //var key = app.globalData.KEY;
    //var paySign = utilsmd5.hexMD5('appId=' + appid + '&nonceStr=' + nonceStr + '&package=' + pkg + '&signType=' + signType + '&timeStamp=' + timeStamp + '&key=' + key).toUpperCase();
    var paySign = param.paySign;   
    wx.requestPayment({
      timeStamp: timeStamp,
      nonceStr: nonceStr,
      package: pkg,
      signType: signType,
      paySign: paySign,
      success: function (res) {
        // success
        console.log("支付")
        //console.log(res)
        wx.redirectTo({
          url: 'orderlist'
        })
      },
      fail: function (res) {
        // fail
        console.log("支付失败")
        console.log(res)
      },
      complete: function () {
        // complete

        that.setData({
          buttonconfirm: true,
          buttonbuy: false
        })
        wx.redirectTo({
          url: 'orderlist'
        })
      }
    })
    console.log("支付 end");
  },

  getlist: function () {
    var that = this;
    var openid = app.globalData.openId;
    var status=that.data.status;
    console.log(status);
    app.util.request({
      'url': 'entry/wxapp/getorder' + '&openid=' + openid + '&status=' + status,
      'cachetime': '30',
      success: function (res) {
        //console.log("success:" + res.data.data.code);

       console.log(res)
        var arraydetail = res.data.data;
        var haveproduct = true;
        that.setData({
          arraydetail: arraydetail,
          haveproduct: haveproduct,

        })


      },
      fail: function (res) {
        console.log("fail:" + res);
      },
    })

  },

  scroll: function (event) {
    //   该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    this.setData({
      scrollTop: event.detail.scrollTop
    });
  },
  getmoredata: function () {
    //   该方法绑定了页面滑动到底部的事件
    this.getlist();
  },
  onLoad: function (options) {
    console.log("onLoad");
    var that = this;
    console.log(that);
    if (options.fromInviteCode) {
      app.globalData.fromInviteCode=options.fromInviteCode;
    }
    app.appstart();
    if (options.orderType) {
      that.setData({ orderType: options.orderType });
    }

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    that.getlist();
  }
})